import 'package:flutter/material.dart';
import 'package:ecy_app_flutter/routes/blind_box/exchange_market/pages/ExchangeRequestListPage.dart';
import 'package:ecy_app_flutter/routes/blind_box/blind_box_myexchange.dart';

class EcySystemNoticePage extends StatefulWidget {
  const EcySystemNoticePage({Key? key}) : super(key: key);

  @override
  State<EcySystemNoticePage> createState() => _EcySystemNoticePageState();
}

class _EcySystemNoticePageState extends State<EcySystemNoticePage> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  final List<String> tabs = ['全部', '物流', '互换'];

  // 示例数据
  final List<Map<String, String>> notices = [
    {
      'type': '互换',
      'direction': 'inbound', // 别人向我申请
      'title': '收到一条互换申请',
      'desc': '谷子酱申请与您交换，点击查看',
      'time': '刚刚',
      'detail': '查看详情',
    },
    {
      'type': '互换',
      'direction': 'outbound', // 我向别人申请
      'title': '互换成功',
      'desc': '恭喜你，与谷子酱互换成功，快去看看吧',
      'time': '1小时前',
      'detail': '查看详情',
    },
    {
      'type': '互换',
      'direction': 'outbound',
      'title': '互换失败',
      'desc': '抱歉，谷子酱拒绝了你的互换请求，点击查看',
      'time': '1小时前',
      'detail': '查看详情',
    },
    {
      'type': '互换',
      'direction': 'outbound',
      'title': '互换请求已发送',
      'desc': '向谷子酱发出互换申请，请耐心等待',
      'time': '1小时前',
      'detail': '查看详情',
    },
  ];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('系统通知'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        bottom: TabBar(
          controller: _tabController,
          tabs: tabs.map((e) => Tab(text: e)).toList(),
          labelColor: Colors.black,
          unselectedLabelColor: Colors.grey,
          indicatorColor: const Color(0xFFF62EBD),
          labelStyle: const TextStyle(fontWeight: FontWeight.w500, fontSize: 15),
        ),
      ),
      backgroundColor: const Color(0xFFF5F5F5),
      body: TabBarView(
        controller: _tabController,
        children: tabs.map((tab) {
          final filtered = tab == '全部'
              ? notices
              : notices.where((n) => n['type'] == tab).toList();
          return ListView.separated(
            padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 0),
            itemCount: filtered.length,
            separatorBuilder: (_, __) => const SizedBox(height: 8),
            itemBuilder: (context, idx) {
              final n = filtered[idx];
              return GestureDetector(
                onTap: () {
                  // 只在互换tab下做区分
                  if (tab == '互换' || n['type'] == '互换') {
                    if (n['direction'] == 'inbound') {
                      // 别人向我申请，跳转到ExchangeRequestListPage
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => ExchangeRequestListPage(),
                        ),
                      );
                    } else {
                      // 我向别人申请，跳转到BlindBoxMyexchange的第二个tab
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => BlindBoxMyexchange(initialTabIndex: 1),
                        ),
                      );
                    }
                  }
                },
                child: Container(
                  margin: const EdgeInsets.symmetric(horizontal: 12),
                  padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 16),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Expanded(
                            child: Text(
                              n['title'] ?? '',
                              style: const TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF262626),
                              ),
                            ),
                          ),
                          Text(
                            n['time'] ?? '',
                            style: const TextStyle(
                              fontSize: 12,
                              color: Color(0xFF999999),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 6),
                      Text(
                        n['desc'] ?? '',
                        style: const TextStyle(
                          fontSize: 14,
                          color: Color(0xFF666666),
                        ),
                      ),
                      const SizedBox(height: 10),
                      Text(
                        n['detail'] ?? '',
                        style: const TextStyle(
                          fontSize: 14,
                          color: Color(0xFFF62EBD),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          );
        }).toList(),
      ),
    );
  }
} 